﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using System.Data;


    public class datTin
    {
        SqlCommand cmd;
        SqlDataReader dr;
        public void createEdit(entTin entT)
        {
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_TinCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[6];

            sqla[0] = new SqlParameter("TinId", DbType.Int32);
            sqla[1] = new SqlParameter("TieuDe", DbType.String);
            sqla[2] = new SqlParameter("NoiDung", DbType.String);
            sqla[3] = new SqlParameter("LoaiTinId", DbType.Int32);
            sqla[4] = new SqlParameter("ThuTu", DbType.Int32);
            sqla[5] = new SqlParameter("NguoiTao", DbType.Int32);
            sqla[0].Value = entT.TinId;
            sqla[1].Value = entT.TieuDe;
            sqla[2].Value = entT.NoiDung;
            sqla[3].Value = entT.LoaiTinId;
            sqla[4].Value = entT.ThuTu;
            sqla[5].Value = entT.NguoiTao;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();

        }
        public List<entTin> getTinByLoaiTinId(int id)
        {
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_TinGetByLoaiTinId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];

            sqla[0] = new SqlParameter("loaitinid", DbType.Int16);
            sqla[0].Value = id;
            cmd.Parameters.AddRange(sqla);

            entTin entT;
            List<entTin> lst = new List<entTin>();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                entT = new entTin();
                entT.LoaiTinId = id;
                entT.TinId = int.Parse(dr["TinId"].ToString());
                entT.TieuDe = dr["tieude"].ToString();
                entT.NoiDung = dr["noidung"].ToString();
                entT.ThuTu = int.Parse(dr["thutu"].ToString());
                entT.NgayTao = dr["ngaytao"].ToString();
                lst.Add(entT);
            }
            return lst;
        }
        public entTin getTinByTinId(int id)
        {
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_TinGetByTinId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("TinId", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entTin entT = new entTin();
            if (dr.Read())
            {
                entT = new entTin();
                entT.TinId = id;
                entT.TieuDe = dr["tieude"].ToString();
                entT.NoiDung = dr["noidung"].ToString();
                entT.ThuTu = int.Parse(dr["thutu"].ToString());
                entT.LoaiTinId=int.Parse(dr["loaitinid"].ToString());
            }
            dr.Close();
            sqlConnection.Close();
            return entT;
        }
    }

